import os

from flask import render_template, Flask, request, make_response, send_from_directory

# 模版
# Jinja2
from werkzeug.utils import secure_filename

app = Flask(__name__)


@app.route('/', methods=['POST', 'GET'])
def home():
    return render_template('www/QQ红包.htm')
    # return render_template('www/filedown.htm')


@app.route('/home2', methods=['POST', 'GET'])
def home2():
    return render_template('www/home2.html', page_list=["1111", "2222", "3333"])


@app.route('/fileUp', methods=['POST', 'GET'])
def fileUp():
    f = request.files['uploadFile']
    basepath = os.path.dirname(__file__)  # 当前文件所在路径
    upload_path = os.path.join(basepath, 'sour',secure_filename(f.filename))
    f.save(upload_path)
    return render_template('www/home2.html', page_list=[request.files["uploadFile"].filename, "上传OK", "3333"])


# @app.route('/sour/<path>')
# def today(path):
#     base_dir = os.path.dirname(__file__)
#     resp = make_response(open(os.path.join(base_dir, path)).read())
#     resp.headers["Content-type"]="text/plan;charset=UTF-8"
#     return resp


@app.route('/<taskid>', methods=['GET'])
def return_file(taskid):
    filename = taskid
    directory = '/'
    response = make_response(send_from_directory(directory, filename, as_attachment=True))
    return response

@app.route('/resp')
def resp():
    response = make_response()
    response.status_code = 304
    return response

if __name__ == '__main__':
    # app.run()
    app.run(host='0.0.0.0', port=5001, debug='True')  # 0.0.0.0表示用本机IP可访问
